//
//  BaseTableViewController.m
//  OSChina
//
//  Created by 8b410-2 on 16/12/26.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCBaseTableViewController.h"
#import "OSCBaseTableViewModel.h"
@interface OSCBaseTableViewController ()
@property (nonatomic, strong, readonly) OSCBaseTableViewModel *viewModel;
@end

@implementation OSCBaseTableViewController

@dynamic viewModel;

- (instancetype)initWithViewModel:(OSCBaseViewModel *)viewModel {
    self = [super initWithViewModel:viewModel];
    if (self) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:self.viewModel.tableViewStyle];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // 没有数据源时 刷新
    if (!self.viewModel.dataSource) {
        [self.tableView.mj_header beginRefreshing];
    }
}

- (void)bindViewModel {
    [super bindViewModel];
    @weakify(self)
    // 数据源改变时 刷新UI
    [[[[RACObserve(self.viewModel, dataSource)
        distinctUntilChanged]
        deliverOnMainThread]
        skip:1]
        subscribeNext:^(NSArray *dataSource) {
            @strongify(self)
            [self reloadData];
     }];
    // 收到刷新信号 执行下拉刷新
    [self.viewModel.needRefresh
     subscribeNext:^(id x) {
        @strongify(self);
        [self refresh];
    }];
    // 收到刷新UI信号 刷新UI
    [[self.viewModel.refreshUI
      deliverOnMainThread]
     subscribeNext:^(id x) {
        @strongify(self);
        [self reloadData];
    }];
    // 如果设置自动处理刷新状态 就自动处理
    if (self.viewModel.autoRefreshStatus) {
        [self handleRefreshStauts];
    }
    
}

- (void)setUpRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
}

- (void)setUpLoadMore {
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

- (void)refresh {
    self.viewModel.listParams.pageIndex = 1;
    [self requestData];
}

- (void)loadMore {
    self.viewModel.listParams.pageIndex++;
    [self requestData];
}

- (void)requestData {
}

- (void)reloadData {
    [self.tableView reloadData];
}

- (void)handleRefreshStauts {
     @weakify(self);
    [[self.viewModel.refreshEndSubject
     deliverOnMainThread]
     subscribeNext:^(id x) {
        @strongify(self);
        [self reloadData];
        switch ([x integerValue]) {
            case OSCHeaderRefresh_HasMoreData: {// 下拉 有更多数据
                [self.tableView.mj_header endRefreshing];
                if (self.tableView.mj_footer == nil) {
                    [self setUpLoadMore];
                }
            }
                break;
            case OSCHeaderRefresh_HasNoMoreData: {// 下拉 没有更多数据
                [self.tableView.mj_header endRefreshing];
                self.tableView.mj_footer = nil;
            }
                break;
            case OSCFooterRefresh_HasMoreData : {// 上拉 有更多数据
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer resetNoMoreData];
                [self.tableView.mj_footer endRefreshing];
            }
                break;
            case OSCFooterRefresh_HasNoMoreData : {// 上拉 没有更多数据
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
                break;
            case OSCRefreshError : {// 错误
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            }
            default:
                break;
        }
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
    return [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self tableView:tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    id object = self.viewModel.dataSource[indexPath.row];
    [self configureCell:cell atIndexPath:indexPath withObject:(id)object];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.viewModel.didSelectCommand execute:indexPath];
}
@end
